﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using ATA.Data.MisaDAL;
using ATA.Business;
using ATA.Data;
using ATA.Entities;

using System.Configuration;

namespace ATA.Winforms
{
    static class Program
    {
        //static frmMain gv_frmMain = new frmMain();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Infragistics.Win.AppStyling.StyleManager.Load(Application.StartupPath + "\\" + "Resources" + "\\" + "ATAOffice2007Blue.isl");

            DAL v_misaDAL = new ATA.Data.MisaDAL.DAL(ConfigurationManager.ConnectionStrings["misaConnectionString"].ConnectionString, ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);

            VwInventoryItemService v_srvProducts = new VwInventoryItemService();

            Globals.gv_lsCustomers = v_misaDAL.GetListCustomer(true);
            
            var allCustomer = new ATA.Data.MisaDAL.AccountingObject();
            allCustomer.AccountingObjectCode = "0";
            allCustomer.AccountingObjectId = Guid.Empty;
            allCustomer.AccountingObjectName = "Tất cả";
            Globals.gv_lsCustomers.Insert(0, allCustomer);

            Globals.gv_lsProducts = v_srvProducts.GetAll();

            frmLogin v_frmLogin = new frmLogin();

            v_frmLogin.ShowDialog();

            if (Globals.gv_strUsername.Trim() != string.Empty)
            {
                #region Load list customers
                KhachHangService v_srvKhachHang = new KhachHangService();
                TList<KhachHang> v_lsKhachang;
                List<AccountingObject> v_lsResult = new List<AccountingObject>();

                if (Globals.gv_bIsGlobalBranch)
                {
                    v_lsKhachang = v_srvKhachHang.GetAll();
                }
                else
                {
                    v_lsKhachang = v_srvKhachHang.Find("BranchID = '" + Globals.gv_strBranchID + "'");
                }

                foreach (KhachHang v_item in v_lsKhachang)
                {
                    AccountingObject v_curCustomer = new AccountingObject();
                    v_curCustomer.AccountingObjectCode = v_item.MaSoHv;
                    v_curCustomer.AccountingObjectId = v_item.KhachHangId;
                    v_curCustomer.AccountingObjectName = v_item.TenKh;
                    v_curCustomer.ParentAccountingObjectId = v_item.ParentId;
                    v_lsResult.Add(v_curCustomer);
                }

                Globals.gv_lsCustomersNotInMisa = v_lsResult; 
                #endregion

                ThuongHieuService v_srvThuongHieu = new ThuongHieuService();
                Globals.gv_lsThuongHieu = v_srvThuongHieu.Find("IsActive = TRUE");

                Application.Run(new frmMain()); 
            }
            //Application.Run(new frmMain());
        }
    }
}
